<?php
/**
 * 管理后台首页
 * 州弟医院信息系统 - 州弟学安全
 */

// 包含函数文件
require_once '../includes/functions.php';

// 检查用户是否已登录并具有管理员权限
if (!isLoggedIn() || !hasRole('admin')) {
    $_SESSION['error_msg'] = '您无权访问该页面';
    redirect('../login.php');
}

// 获取统计信息
$totalUsers = 0;
$totalPatients = 0;
$totalDoctors = 0;
$totalAppointments = 0;
$recentAppointments = [];

// 获取用户总数
$sql = "SELECT COUNT(*) AS total FROM users";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $totalUsers = $row['total'];
}

// 获取患者总数
$sql = "SELECT COUNT(*) AS total FROM patients";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $totalPatients = $row['total'];
}

// 获取医生总数
$sql = "SELECT COUNT(*) AS total FROM doctors";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $totalDoctors = $row['total'];
}

// 获取预约总数
$sql = "SELECT COUNT(*) AS total FROM appointments";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $totalAppointments = $row['total'];
}

// 获取最近预约
$sql = "SELECT a.*, p.name AS patient_name, d.name AS doctor_name, dept.name AS department_name 
        FROM appointments a 
        JOIN patients p ON a.patient_id = p.id 
        JOIN doctors d ON a.doctor_id = d.id 
        JOIN departments dept ON a.department_id = dept.id 
        ORDER BY a.appointment_time DESC LIMIT 5";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $recentAppointments[] = $row;
    }
}

// 获取最近登录记录
$recentLogs = [];
$sql = "SELECT l.*, u.username 
        FROM system_logs l 
        LEFT JOIN users u ON l.user_id = u.id 
        ORDER BY l.created_at DESC LIMIT 10";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $recentLogs[] = $row;
    }
}

?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="/assets\css/bootstrap_69c9e605.min.css">
    <link rel="stylesheet" href="/assets\css/all_3b858821.min.css">
    <link rel="stylesheet" href="/assets\css/style.css">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 px-0 admin-sidebar">
                <div class="py-3 px-3 bg-dark">
                    <a href="<?php echo SITE_URL; ?>admin/" class="text-white text-decoration-none">
                        <i class="fas fa-hospital-alt mr-2"></i>管理后台
                    </a>
                </div>
                <div class="p-3">
                    <div class="text-white mb-3">
                        <i class="fas fa-user-circle mr-1"></i> <?php echo $_SESSION['username']; ?>
                    </div>
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link active" href="<?php echo SITE_URL; ?>admin/">
                                <i class="fas fa-tachometer-alt mr-2"></i>仪表盘
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/users.php">
                                <i class="fas fa-users mr-2"></i>用户管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/patients.php">
                                <i class="fas fa-user-injured mr-2"></i>患者管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/doctors.php">
                                <i class="fas fa-user-md mr-2"></i>医生管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/departments.php">
                                <i class="fas fa-hospital mr-2"></i>科室管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/appointments.php">
                                <i class="fas fa-calendar-check mr-2"></i>预约管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/medicines.php">
                                <i class="fas fa-pills mr-2"></i>药品管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/records.php">
                                <i class="fas fa-notes-medical mr-2"></i>病历管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/logs.php">
                                <i class="fas fa-history mr-2"></i>系统日志
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/settings.php">
                                <i class="fas fa-cog mr-2"></i>系统设置
                            </a>
                        </li>
                        <li class="nav-item mt-3">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>">
                                <i class="fas fa-home mr-2"></i>返回前台
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>logout.php">
                                <i class="fas fa-sign-out-alt mr-2"></i>退出登录
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
            
            <!-- 主内容区 -->
            <div class="col-md-10 ml-sm-auto px-4 py-3">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">管理后台</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="dropdown">
                            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <i class="fas fa-cog"></i> 操作
                            </button>
                            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                <a class="dropdown-item" href="<?php echo SITE_URL; ?>admin/profile.php">个人设置</a>
                                <a class="dropdown-item" href="<?php echo SITE_URL; ?>admin/settings.php">系统设置</a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="<?php echo SITE_URL; ?>logout.php">退出登录</a>
                            </div>
                        </div>
                    </div>
                </div>
                
                <?php if (isset($_SESSION['admin_success_msg'])): ?>
                    <div class="alert alert-success">
                        <?php echo $_SESSION['admin_success_msg']; unset($_SESSION['admin_success_msg']); ?>
                    </div>
                <?php endif; ?>
                
                <?php if (isset($_SESSION['admin_error_msg'])): ?>
                    <div class="alert alert-danger">
                        <?php echo $_SESSION['admin_error_msg']; unset($_SESSION['admin_error_msg']); ?>
                    </div>
                <?php endif; ?>
                
                <!-- 概览卡片 -->
                <div class="row">
                    <div class="col-md-3">
                        <div class="admin-dashboard-card">
                            <div class="card-icon text-primary">
                                <i class="fas fa-users"></i>
                            </div>
                            <h5>用户总数</h5>
                            <div class="card-value"><?php echo $totalUsers; ?></div>
                            <a href="<?php echo SITE_URL; ?>admin/users.php" class="btn btn-sm btn-outline-primary mt-2">查看详情</a>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="admin-dashboard-card">
                            <div class="card-icon text-success">
                                <i class="fas fa-user-injured"></i>
                            </div>
                            <h5>患者总数</h5>
                            <div class="card-value"><?php echo $totalPatients; ?></div>
                            <a href="<?php echo SITE_URL; ?>admin/patients.php" class="btn btn-sm btn-outline-success mt-2">查看详情</a>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="admin-dashboard-card">
                            <div class="card-icon text-info">
                                <i class="fas fa-user-md"></i>
                            </div>
                            <h5>医生总数</h5>
                            <div class="card-value"><?php echo $totalDoctors; ?></div>
                            <a href="<?php echo SITE_URL; ?>admin/doctors.php" class="btn btn-sm btn-outline-info mt-2">查看详情</a>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="admin-dashboard-card">
                            <div class="card-icon text-warning">
                                <i class="fas fa-calendar-check"></i>
                            </div>
                            <h5>预约总数</h5>
                            <div class="card-value"><?php echo $totalAppointments; ?></div>
                            <a href="<?php echo SITE_URL; ?>admin/appointments.php" class="btn btn-sm btn-outline-warning mt-2">查看详情</a>
                        </div>
                    </div>
                </div>
                
                <!-- 内容区域 -->
                <div class="row mt-4">
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="mb-0">最近预约</h5>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>患者</th>
                                                <th>医生</th>
                                                <th>科室</th>
                                                <th>时间</th>
                                                <th>状态</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if (empty($recentAppointments)): ?>
                                                <tr>
                                                    <td colspan="6" class="text-center">暂无预约记录</td>
                                                </tr>
                                            <?php else: ?>
                                                <?php foreach ($recentAppointments as $appointment): ?>
                                                    <tr>
                                                        <td><?php echo $appointment['id']; ?></td>
                                                        <td><?php echo $appointment['patient_name']; ?></td>
                                                        <td><?php echo $appointment['doctor_name']; ?></td>
                                                        <td><?php echo $appointment['department_name']; ?></td>
                                                        <td><?php echo date('Y-m-d H:i', strtotime($appointment['appointment_time'])); ?></td>
                                                        <td>
                                                            <?php
                                                            $status_class = '';
                                                            switch ($appointment['status']) {
                                                                case '待确认':
                                                                    $status_class = 'badge-warning';
                                                                    break;
                                                                case '已确认':
                                                                    $status_class = 'badge-info';
                                                                    break;
                                                                case '已完成':
                                                                    $status_class = 'badge-success';
                                                                    break;
                                                                case '已取消':
                                                                    $status_class = 'badge-danger';
                                                                    break;
                                                            }
                                                            ?>
                                                            <span class="badge <?php echo $status_class; ?>"><?php echo $appointment['status']; ?></span>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="card-footer text-center">
                                <a href="<?php echo SITE_URL; ?>admin/appointments.php" class="btn btn-sm btn-outline-primary">查看全部</a>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="mb-0">系统日志</h5>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>用户</th>
                                                <th>操作</th>
                                                <th>IP地址</th>
                                                <th>时间</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if (empty($recentLogs)): ?>
                                                <tr>
                                                    <td colspan="4" class="text-center">暂无日志记录</td>
                                                </tr>
                                            <?php else: ?>
                                                <?php foreach ($recentLogs as $log): ?>
                                                    <tr>
                                                        <td><?php echo $log['username'] ?? '未知用户'; ?></td>
                                                        <td><?php echo $log['action']; ?></td>
                                                        <td><?php echo $log['ip_address']; ?></td>
                                                        <td><?php echo date('Y-m-d H:i', strtotime($log['created_at'])); ?></td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="card-footer text-center">
                                <a href="<?php echo SITE_URL; ?>admin/logs.php" class="btn btn-sm btn-outline-primary">查看全部</a>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 版权信息 -->
                <footer class="mt-5 text-center text-muted">
                    <p>
                        &copy; <?php echo date('Y'); ?> <?php echo SITE_NAME; ?>. 版权所有
                        <br>
                        <small>Powered by <a href="#" class="text-muted">州弟学安全</a> | 版本 <?php echo VERSION; ?></small>
                    </p>
                </footer>
            </div>
        </div>
    </div>

    <script src="/assets\js/jquery-3_7b53a2dc.5.1.min.js"></script></script></script></script>
    <script src="/assets\js/popper_9c21e708.min.js"></script></script></script></script>
    <script src="/assets\js/bootstrap_a618e9ff.min.js"></script></script></script></script>
    <script src="/assets\js/chart_a54fe656.min.js"></script></script></script></script>
    <script src="/assets\js/main.js"></script></script>
</body>
</html> 